//
//  ActivityViewController.m
//  UI_TabBarTest1(豆瓣)
//
//  Created by dllo on 16/6/23.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "ActivityViewController.h"
#import "Activity.h"
#import "ActivityListTableViewCell.h"
#import "ActivityDetailViewController.h"
@interface ActivityViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)NSMutableArray *activityArray;

@end

@implementation ActivityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.activityArray = [NSMutableArray arrayWithCapacity:5];
    
    
    [self setNavi];
    [self handleData];
    [self createTableView];
}

-(void)setNavi{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav"] forBarMetrics:0];
    self.title = @"活动";
}

-(void)handleData{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"activitylist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSArray *eventArray = [dictionary valueForKey:@"events"];
    for (NSDictionary *dic in eventArray){
        Activity *activity = [[Activity alloc] init];
        [activity setValuesForKeysWithDictionary:dic];
        [self.activityArray addObject:activity];
    }
  //  NSLog(@"%@",self.activityArray);
}

-(void)createTableView{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, 0, self.view.frame.size.width - 40, self.view.frame.size.height - 49) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    tableView.rowHeight = 230.0;
    tableView.showsHorizontalScrollIndicator = false;
    tableView.showsVerticalScrollIndicator = false;
    [tableView registerClass:[ActivityListTableViewCell class] forCellReuseIdentifier:@"activityListReuse"];
    [tableView release];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.activityArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ActivityListTableViewCell *activityListCell = [tableView dequeueReusableCellWithIdentifier:@"activityListReuse" forIndexPath:indexPath];
    activityListCell.activity = [self.activityArray objectAtIndex:indexPath.row];
    return activityListCell;
}
/** 选中一个cell的时候跳转 */
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Activity *activity = [self.activityArray objectAtIndex:indexPath.row];
    ActivityDetailViewController *activityDetailVC = [[ActivityDetailViewController alloc] init];
    activityDetailVC.activity = activity;
    [self.navigationController pushViewController:activityDetailVC animated:YES];
    [activityDetailVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
